<template>
	<view>
		<scroll-view scroll-y="true" :style="{height: windowHeight+'px'}" @scrolltolower="lower">
			<view class="good-panel" v-for="(item,index) in list" :key='index'>
				<LimitGoods :goods='item'></LimitGoods>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import LimitGoods from './limitGoods.vue'
	import {
		toast,
		debounce
	} from '@/utils/utils.js'
	export default {
	
		props: {
			// 1今日气价 2 燃气配件 
			category: {
				type: [Number, String],
				default: 1
			},
			// 用气类型：0-民用 1-商业 2-工业
			userType: {
				type: [Number, String],
				default: 0
			},
			refreshIndex: {
				type: Boolean,
				default: false
			},
			windowHeight: {
				type: Number,
				default: 0
			},
		},
		watch: {
			refreshIndex: {
				immediate: true,
				handler: function(val) {
					console.log('refreshIndex：',val)
					
					this.initData();
					
				}
			},
			category: {
				immediate: true,
				handler: function(val) {
					if (val) {
						
						this.categoryType = val;
						console.log('商品类型：',val)
						this.initData();
					}
				}
			},
			userType: {
				immediate: true,
				handler: function(val) {
					if (val) {
						console.log('用户类型：',val)
						
						this.type = val;
						
						this.initData();
					}
				}
			}
		},
		data() {
			return {
				categoryType:1,
				list: [],
				type: 0,
				lpgUserName: "",
				limit: 10,
				page: 1,
				total: 0,
				loadStatus: 'more', 
				isLoadMore:false,
				isLoadDataBool:false
			};
		},

		components: {
			LimitGoods
		},
		mounted() {

		},

		methods: {
			lower(){
				if (!this.isLoadMore) { //此处判断，上锁，防止重复请求
					if (this.total > (this.limit * this.page)) {
						this.isLoadMore = true;
						this.page += 1
							this.initData();	
						}
				}
			},
			
			initData:debounce(function(){
				uni.showLoading();
				const params = {
					page: this.page,
					limit: this.limit,
					type: this.type,
					category: this.categoryType,
					name: this.lpgUserName
				};
				this.$API.getGoodsList(params).then(res => {
					uni.hideLoading();
					this.isLoadDataBool = false;
					if (this.page !== 1) {
						this.list = this.list.concat(res.page.list || [])
					} else {
						this.list = res.page.list || [];
					}
					this.total = res.page.totalCount;
					uni.stopPullDownRefresh(); //关闭加载动画
					if (this.list.length == this.total) { //判断接口返回数据量小于请求数据量，则表示此为最后一页
						this.isLoadMore = true
						this.loadStatus = 'nomore'
					} else {
						this.isLoadMore = false
					}
				}).catch(() => {
					//联网失败, 结束加载
				});
			},100) 
		}


	}
</script>

<style scoped lang="scss">
	.good-panel {
		margin: 0rpx 0 24rpx;
	}
</style>